#ifndef __CLION_CPPTEST_GLUTTONOUS_SNAKE_H__
#define __CLION_CPPTEST_GLUTTONOUS_SNAKE_H__
#include <windows.h>
#include "../Utils/KeyBoardUtils.h"

static const SHORT width = 90;
static const SHORT height = 30;
//#define SNAKE_LEN (width * height)    //这样动态定义都不行...
#define SNAKE_LEN 2700

/**
 * 设置控制台光标位置
 * @param x
 * @param y
 */
extern WINBOOL setConsoleCursorPosition(SHORT x, SHORT y);
extern WINBOOL setConsoleCursorInfo(BOOLEAN visible);

struct Snake {
    //蛇长度
    int size;
    //身体坐标 数组
    COORD bodys[SNAKE_LEN];
};

//蛇
static struct Snake snake;
//食物坐标
static SHORT food[2] = {0};
//分数, 每次+10
static int score = 0;
//游戏是否已停止
static int isGameOver = 0;
//是否暂停
static int isPause = 0;
//方向wsad, 小写
static char oritation = 'w';
//每走1步所花时间
static int stepTime = 200;

static void drawFood();
static void initSnake();
static void drawSnake(COORD *tail);
static void deal_pressed_key(struct Keyboard_Key pKey);
static void draw_wall_text(); //4.画墙&文字说明
//添加键盘输入监听 & 循环
static void playGame();
struct Keyboard_Key getKeyInput(int isPrintLog);

//判断是否撞墙 or 是否撞到自己身体
static WINBOOL is_bump_wall_or_self() {
    COORD head = snake.bodys[0];
    if (head.X < 0 || head.X == width || head.Y < 0 || head.Y == height) {
        return TRUE;
    }
    //是否撞到自己身体
    for (int i = 1; i < snake.size; ++i) {
        COORD bodyi = snake.bodys[i];
        if (head.X == bodyi.X && head.Y == bodyi.Y) {
            return TRUE;
        }
    }
    return FALSE;
}

//3.判断是否吃到食物, if吃到就增加长度&分数, 重新生成食物
static BOOLEAN judge_eat_food(SHORT tailX, SHORT tailY) {
    COORD head = snake.bodys[0];
    if (head.X == food[0] && head.Y == food[1]) {
         snake.size++;
//        head.X = snake.bodys[1].X;
        snake.bodys[snake.size - 1].X = tailX;
        snake.bodys[snake.size - 1].Y = tailY;
        score += 10;

        setConsoleCursorPosition(0, height + 5);
        printf("当前分数: %d\n", score);
        return TRUE;
    }
    return FALSE;
}

/**
 * 坐标是否与蛇重叠
 * @param x
 * @param y
 * @return 1是, 0否
 */
static WINBOOL is_overlap_snake(int x, int y) {
    for (int i = 0; i < snake.size; ++i) {
        SHORT X = snake.bodys[i].X;
        SHORT Y = snake.bodys[i].Y;
        if (x == X && y == Y) return TRUE;
    }
    return FALSE;
}

#endif //__CLION_CPPTEST_GLUTTONOUS_SNAKE_H__
